<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class CertifiedDrivingAgent
 * Desc  认证信息控制器
 * Create on 2024/10/22 15:34
 * Create by wangyafang
 */

namespace app\api\controller\wddj\driver;


use addons\wddj\library\IDCardEncryptor;
use app\api\controller\wddj\BaseController;
use app\api\model\wddj\User;
use think\Db;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class CertifiedDrivingAgent extends BaseController
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['submit','details'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\driver\CertifiedDrivingAgent();
    }

    /**
     * Desc 提交
     * Create on 2024/10/22 15:35
     * Create by wangyafang
     */
    public function submit()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);

        $param = $this->request->post();
        if (!isset($param['district']) || (isset($param['district']) && empty($param['district']))) {
            $this->error('所属地区不能为空');
        }
        $cityObj = $this->validateCity($param['district']);
        $param['city_id'] = $cityObj['id'];
        $create_data = array(
            'wechat_id'=>$wechat_id,
        );
        $create_data = array_merge($create_data,$param);
        $result = $this->validate($create_data,'app\api\validate\wddj\driver\CertifiedDrivingAgent');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $certifiedDrivingAgentObj = $this->model->where('wechat_id',$wechat_id)->find();
        if ($certifiedDrivingAgentObj && $certifiedDrivingAgentObj['state'] == '1') {
            $this->error('认证代驾正在审核，不能重复提交');
        }
        $configObj = (new \app\api\model\wddj\Config())->get(1);
        $encryptor = new IDCardEncryptor($configObj['encryptor_key'],$configObj['encryptor_iv']);
        $create_data['id_card'] = $encryptor->encrypt($create_data['id_card']);
        $certifiedDrivingAgentObjCheck = $this->model
            ->where('wechat_id','<>',$wechat_id)
            ->where('id_card',$create_data['id_card'])
            ->find();
        if ($certifiedDrivingAgentObjCheck) {
            $this->error('身份信息已被使用');
        }

        if ($certifiedDrivingAgentObj) {
            unset($create_data['state']);
            unset($create_data['reject']);
            $update_data = array_merge(array(
                'state'=>'1',
                'reject'=>'',
            ),$create_data);
            Db::startTrans();
            try {
                $certifiedDrivingAgentObj->save($update_data);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
        } else {
            Db::startTrans();
            try {
                $this->model->data($create_data);
                $this->model->allowField(true)->save();
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
        }

        $this->success('提交成功');
    }
    
    /**
     * Desc 详情
     * Create on 2024/10/22 15:35
     * Create by wangyafang
     */
    public function details()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }

        $wechat_id = User::getWechatId($this->auth->id);

        $certifiedDrivingAgentObj = $this->model
            ->where('wechat_id',$wechat_id)
            ->field('name,mobile,id_card,driving_experience,driver_license_type,
            front_id_card,front_vehicle_driver_license,state,reject,
            district')
            ->find();

        if ($certifiedDrivingAgentObj && $certifiedDrivingAgentObj['state'] != '3') {
            $certifiedDrivingAgentObj->hidden(['reject']);
        }

        $this->success('请求成功',$certifiedDrivingAgentObj);
    }
}



 